'use strict';

const Service = require('egg').Service;

class RoomService extends Service {
  async prepare(arg) { // 准备
    const { app } = this;
    const room = await app.mysql.get('room', { roomnum: arg.roomNum });
    room.prepare += 1;
    await app.mysql.update('room', room);
  }

  async start(arg) { // 开始
    const { app, ctx } = this;
    const room = await app.mysql.get('room', { roomnum: arg.roomNum });
    if (room.prepare === room.peoplenum) { // 玩家全部准备完毕
      const active = room.active.split('|');
      const at = active[room.at];
      const userObj = await this.service.operation.player(active);
      ctx.socket.emit('wager', { userObj });
      ctx.socket.in(arg.roomNum).emit('wager', { userObj });
      ctx.socket.emit('beton', { at });
      ctx.socket.in(arg.roomNum).emit('beton', { at });
      await app.mysql.update('room', room);
    } else {
      await ctx.socket.emit(arg.id, { msg: '还有玩家未准备！' });
    }
  }
}
module.exports = RoomService;
